#!/bin/sh

test -f /usr/sbin/lircd || exit 0
test -f /etc/lircd.conf || exit 0

# if /var/run does not exist, we are screwed anyway...
test -d /var/run/lirc || mkdir /var/run/lirc

boxver()
{
	if test -z "$STB_ID"; then
		eval $(sed 's/^.* \(STB_ID[^[:space:]]*\).*$/\1/' /proc/cmdline)
	fi
	BOX_ID=${STB_ID:0:8}	# 09:00:07
	BOX_ID=${BOX_ID//:/_}	# 09_00_07
	export BOX_ID
}

case "$1" in
  start)
    echo -n "Starting lirc daemon: lircd"
    boxver	# determine boxtype
    # if a /etc/lircd.conf.$BOX_ID exists, use it...
    test -z "$LIRCD_OPTS" && test -e /etc/lircd.conf.$BOX_ID && LIRCD_OPTS=/etc/lircd.conf.$BOX_ID
    start-stop-daemon --start --quiet --exec /usr/sbin/lircd -- -u --device=/dev/lirc0 $LIRCD_OPTS
    echo "."
    ;;
  stop)
    echo -n "Stopping lirc daemon: lircd"
    start-stop-daemon --stop --quiet --exec /usr/sbin/lircd
    echo "."
    ;;
  reload|force-reload)
    start-stop-daemon --stop --quiet --signal 1 --exec /usr/sbin/lircd
    ;;
  restart)
    echo -n "Stopping lirc daemon: lircd"
    $0 stop
    sleep 1
    echo -n "Starting lirc daemon: lircd"
    $0 start
    echo "."
    ;;
  *)
    echo "Usage: /etc/init.d/lircd {start|stop|reload|restart|force-reload}"
    exit 1
esac

exit 0
